package iotwisp.base.cqrs.unitofwork;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 工作单元
 * 
 * @author yuxx
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class UnitOfWork {
	
	@SuppressWarnings("rawtypes")
//	private List<RabbitMQBaseEvent> transientEvents = new ArrayList<RabbitMQBaseEvent>();

	/**
	 * 记录器id
	 */
	private String id;

	/**
	 * 流程id
	 */
	private String sagaId;

	/**
	 * 根据命令id获取一个事件日志记录器实例
	 * 
	 * @param commandId
	 * @return
	 */
	public static UnitOfWork getInstance(String id, String sagaId) {

		UnitOfWork unitOfWork = new UnitOfWork();
		unitOfWork.id = id;
		unitOfWork.sagaId = sagaId;
		return unitOfWork;
	}

	/**
	 * 产生一个业务事件
	 * @param exchange
	 * @param describe
	 * @param topic
	 * @param topicKey
	 * @param payload
	 * @param developer
	 * @return
	 */
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	public BaseEvent generateEvent(String exchange, String describe, String topic, String topicKey, Object payload, String developer) {
//
//		RabbitMQBaseEvent<Object> event = new RabbitMQBaseEvent(exchange, topic, topicKey, describe, payload, developer);
//		transientEvents.add(event);
//
//		return event;
//	}

	/**
	 * 产生业务事件（包含补充参数）
	 * @param exchange
	 * @param describe
	 * @param topic
	 * @param topicKey
	 * @param payload
	 * @param developer
	 * @param map
	 * @return
	 */
//	public BaseEvent generateEvent(String exchange, String describe, String topic, String topicKey, Object payload, String developer, Map<String,String> map) {
//
//		RabbitMQBaseEvent<Object> event = new RabbitMQBaseEvent(exchange, topic, topicKey, describe, payload, developer,map);
//		transientEvents.add(event);
//
//		return event;
//	}

	/**
	 * 工作单元处理完以后将所有事件发布到MQ，日志写入ES
	 * 
	 * @param clear
	 */
//	@SuppressWarnings("rawtypes")
//	public void publish(boolean clear) {
//
//		// 把业务事件推送到MQ
//		try {
//			log.info("aop准备推送消息事件{}", JSONUtils.c(transientEvents));
//			RabbitMQEventUtil.publish(transientEvents);
//		} catch (TimeoutException e) {
//			log.error(e.getMessage(), e);
//		}
//
//		// 清空
//		transientEvents = new ArrayList<RabbitMQBaseEvent>();
//	}

}
